{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0.9390,  0.9945, -0.6318, -0.3996, -0.4461,  0.7764,  0.6432, -0.9876,\n",
       "        -0.2016, -0.0692])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.empty([10], dtype=torch.float).uniform_(-1, 1)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([2., 3., 1., 0., 4.])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.histc(x, 5, -1, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([2., 3., 1., 0., 4.])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def histc(t:torch.Tensor, bins, min, max):\n",
    "    ret = torch.empty([bins], dtype=t.dtype).zero_()\n",
    "    delta = (max - min) / bins\n",
    "    for i in range(bins):\n",
    "        l = t >= min + i * delta\n",
    "        b = t <= min + (i + 1) * delta\n",
    "        ret[i] = l[b].sum()\n",
    "    return ret\n",
    "histc(x, 5, -1, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([4., 6., 3., 8., 9.])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.empty([10, 3], dtype=torch.float).uniform_(-1, 1)\n",
    "torch.histc(x, 5, -1, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.4\n",
      "tensor([-0.], dtype=torch.float16)\n",
      "tensor([1.1921e-07], dtype=torch.float16)\n",
      "tensor([-10.0000,  -9.6016,  -9.2031,  -8.7969,  -8.3984,  -8.0000,  -7.6016,\n",
      "         -7.1992,  -6.8008,  -6.3984,  -6.0000,  -5.6016,  -5.1992,  -4.8008,\n",
      "         -4.3984,  -4.0000,  -3.5996,  -3.1992,  -2.8008,  -2.4004,  -2.0000,\n",
      "         -1.5996,  -1.2002,  -0.7998,  -0.3999,  -0.0000,   0.3999,   0.7998,\n",
      "          1.2002,   1.5996,   2.0000,   2.4004,   2.8008,   3.1992,   3.5996,\n",
      "          4.0000,   4.3984,   4.8008,   5.1992,   5.6016,   6.0000,   6.3984,\n",
      "          6.8008,   7.1992,   7.6016,   8.0000,   8.3984,   8.7969,   9.2031,\n",
      "          9.6016], dtype=torch.float16)\n"
     ]
    }
   ],
   "source": [
    "step = 20 / 50\n",
    "v = torch.arange(-10, 10, 20 / 50, dtype=torch.float).to(torch.half)\n",
    "v2 = torch.tensor([-1.0000e+01, -9.6016e+00, -9.2031e+00, -8.7969e+00, -8.3984e+00,\n",
    "        -8.0000e+00, -7.6016e+00, -7.1992e+00, -6.8008e+00, -6.3984e+00,\n",
    "        -6.0000e+00, -5.6016e+00, -5.1992e+00, -4.8008e+00, -4.3984e+00,\n",
    "        -4.0000e+00, -3.5996e+00, -3.1992e+00, -2.8008e+00, -2.4004e+00,\n",
    "        -2.0000e+00, -1.5996e+00, -1.2002e+00, -7.9980e-01, -3.9990e-01,\n",
    "         1.1921e-07,  3.9990e-01,  7.9980e-01,  1.2002e+00,  1.5996e+00,\n",
    "         2.0000e+00,  2.4004e+00,  2.8008e+00,  3.1992e+00,  3.5996e+00,\n",
    "         4.0000e+00,  4.3984e+00,  4.8008e+00,  5.1992e+00,  5.6016e+00,\n",
    "         6.0000e+00,  6.3984e+00,  6.8008e+00,  7.1992e+00,  7.6016e+00,\n",
    "         8.0000e+00,  8.3984e+00,  8.7969e+00,  9.2031e+00,  9.6016e+00],\n",
    "       dtype=torch.float16)\n",
    "ret = v != v2\n",
    "print(step)\n",
    "print(v[ret])\n",
    "print(v2[ret])\n",
    "torch.where(ret)\n",
    "print(v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
